package cn.math.appmath.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.math.appmath.bean.PageBean;
import cn.math.appmath.bean.TrenderBean;
import cn.math.appmath.dao.SomeConstants;
import cn.math.appmath.dao.TrenderDao;
import cn.math.appmath.entity.Trender;

/**
 * A data access object (DAO) providing persistence and search support for
 * Trender entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cn.math.appmath.entity.Trender
 * @author MyEclipse Persistence Tools
 */

public class TrenderDaoImpl extends HibernateDaoSupport implements TrenderDao {
	private static final Log log = LogFactory.getLog(TrenderDaoImpl.class);

	public boolean save(Trender transientInstance) {
		log.debug("attach Trender instance");
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.saveOrUpdate(transientInstance);
			tx.commit();
			session.close();
			log.debug("attach successful");
			return true;
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			re.printStackTrace();
			return false;
		}
	}

	public boolean batchDelete(Integer[] ids) {
		log.debug("batchDelete Trender instance");
		try{
			String hql = "delete from Trender where tno in (:ids)";
			Query query = getSession().createQuery(hql);
			query.setParameterList("ids", ids);
			query.executeUpdate();
			log.debug("batchDelete successful");
			return true;
		}catch (Exception ex){
			log.error("batchDelete failed", ex);
			ex.printStackTrace();
			return false;
		}
	}
	
	public void delete(Trender persistentInstance) {
		log.debug("deleting Trender instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Trender findById(java.lang.Integer id) {
		log.debug("getting Trender instance with id: " + id);
		try {
			Trender instance = (Trender) getSession().get(
					"cn.math.appmath.entity.Trender", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Trender> findByExample(Trender instance) {
		log.debug("finding Trender instance by example");
		try {
			List<Trender> results = (List<Trender>)getSession()
					.createCriteria("cn.math.entity.Trender").add(
							Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List<Trender> findByProperty(String propertyName, Object value) {
		log.debug("finding Trender instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Trender as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return (List<Trender>)queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List<Trender> findTop(){
		log.debug("finding top 2 Trender");
		try{
			String queryString = "from Trender order by tcreateTime desc";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setFirstResult(0);
			queryObject.setMaxResults(2);
			return (List<Trender>)queryObject.list();
		}catch(RuntimeException re){
			log.error("find top 2 failed", re);
			re.printStackTrace();
			return null;
		}
	}
	
	public TrenderBean findAll(int page) {
		log.debug("finding all Trender instances");
		try {
			String queryString = "select count(*) from Trender";
			Query queryObject = getSession().createQuery(queryString);
			int allRow = Integer.parseInt(String.valueOf(queryObject.uniqueResult()));
			int totalPage = PageBean.countTotalPage(SomeConstants.PageSize, allRow);
			int currentPgae;
			if(page == totalPage)
				currentPgae = totalPage;
			else
				currentPgae = PageBean.countCurrentPage(page);
			int currentRow = PageBean.countCurrentRow(SomeConstants.PageSize, currentPgae);
			queryString = "from Trender order by tcreateTime desc";
			queryObject = getSession().createQuery(queryString);
			queryObject.setFirstResult(currentRow);
			queryObject.setMaxResults(SomeConstants.PageSize);
			List<Trender> list;
			if(allRow != 0){
				list = (List<Trender>)queryObject.list();
			}else{
				list = null;
			}
			return new TrenderBean(totalPage, currentPgae, list, allRow);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
}